/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.glidebitmappoolsample;

import com.glidebitmappool.GlideBitmapPool;

import com.glidebitmappoolsample.slice.MainAbilitySlice;
import com.glidebitmappoolsample.util.FileUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 主页
 *
 * @author renpengfei
 * @since 2021-03-12
 */
public class MainAbility extends Ability {
    private static final int PERMISSION_REQUEST_CODE = 0;
    private static final String WRITE_USER_STORAGE = "ohos.permission.WRITE_USER_STORAGE";
    private static final String READ_USER_STORAGE = "ohos.permission.READ_USER_STORAGE";
    private static final int MAX_SIZE = 2 * 1024 * 1024;
    private String mRequiredError1 = null;
    private String mRequiredError2 = null;
    private String mRequiredSuccess = null;

    private HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x001, "TAG");

    /**
     * 主页初始化
     *
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());

        FileUtils.copy2Memory(getContext(), "icon.png");
        for (int i1 = 1; i1 <= Constants.TWENTY; i1++) {
            String path = "test" + i1 + ".png";
            FileUtils.copy2Memory(getContext(), path);
        }

        GlideBitmapPool.initialize(MAX_SIZE);

        mRequiredError1 = getString(ResourceTable.String_image_access_required);
        mRequiredError2 = getString(ResourceTable.String_storage_permission_denied);
        mRequiredSuccess = getString(ResourceTable.String_image_access_success);

        requestPermission();
    }

    private void requestPermission() {
        if (verifyCallingPermission(WRITE_USER_STORAGE) != IBundleManager.PERMISSION_GRANTED
                || verifyCallingPermission(READ_USER_STORAGE) != IBundleManager.PERMISSION_GRANTED) {
            if (canRequestPermission(WRITE_USER_STORAGE)
                    || canRequestPermission(READ_USER_STORAGE)) {
                requestPermissionsFromUser(
                        new String[]{WRITE_USER_STORAGE, READ_USER_STORAGE}, PERMISSION_REQUEST_CODE);
            } else {
                HiLog.error(logLabel, mRequiredError1);
                new ToastDialog(getContext()).setContentText(mRequiredError1).show();
            }
        } else {
            HiLog.error(logLabel, mRequiredError2);
            new ToastDialog(getContext()).setContentText(mRequiredError2).show();
        }
    }

    /**
     * 请求权限回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && (grantResults[0] == IBundleManager.PERMISSION_GRANTED
                        && grantResults[1] == IBundleManager.PERMISSION_GRANTED)) {
                    HiLog.error(logLabel, mRequiredSuccess);
                    new ToastDialog(getContext()).setContentText(mRequiredSuccess).show();
                } else {
                    HiLog.error(logLabel, mRequiredError2);
                    new ToastDialog(getContext()).setContentText(mRequiredError2).show();
                }
                return;
            default:
                break;
        }
    }
}
